<?php

namespace app\shop\controller;

use think\Controller;
use app\common\library\Email;
use think\cache\driver\Redis;
// 引入类
use app\common\library\VerifyCodeManager;
class Glogin extends Controller
{
    //构造函数
    public function __construct()
    {
        parent::__construct();
        //加载模型
        $this->userModel = model('common/User/User');
    }

    // 登录功能
    public function getLogin(){
        if($this->request->isPost()){
            // 获取前端传过来的username 和 password的值  --trim是清除前后空格
            $userId=$this->request->param('userid');
            $userMobile=$this->request->param('usermobile');
            $userEmail=$this->request->param('useremail');
            if($userEmail===''){
                $query=$this->userModel;
            }else{
                $query=$this->userModel->where('email',$userEmail);
            }

            if(($userId!=='') && ($userMobile!=='')){
                $data=[
                    'id'=>$userId,
                    'mobile'=>$userMobile
                ];
                $isLoginRes = $query->where($data)->select();
                if($isLoginRes){
                    $this->success('用户存在',null,$isLoginRes);
                    exit;
                }else{
                    $this->error('用户不存在',null,$isLoginRes);
                    exit;
                }
            }else{
                $this->error('用户ID和手机号不能为空',null);
                exit;
            }

        }else{
            $this->error('请求错误');
        }
    }

    // 忘记密码-发送验证码
    public function forgetPwd(){
        if($this->request->isPost()){
            $emails = $this->request->param('email');
            // $uid = $this->request->param('uid');
            // $mobile = $this->request->param('mobile');
            // halt($emails);
            if($emails ==''){
                $this->error('邮箱不能为空');
                exit;
            }
            $where = [
                // 'id'=>$uid,
                // 'mobile'=>$mobile,
                'email'=>$emails
            ];
            $isUser = $this->userModel->where($where)->find();
            // $selectEmail = $this->userModel->get(['email'=>$emails]);
            // halt($isUser);
            if($isUser==NULL){
                $this->error('该邮箱未注册，请前往注册页面');
                exit;
            }else{
                $row = [
                    'mail_type'=>config('site.mail_type'),
                    'mail_smtp_host'=>config('site.mail_smtp_host'),
                    'mail_smtp_port'=>config('site.mail_smtp_port'),
                    'mail_smtp_user'=>config('site.mail_smtp_user'),
                    'mail_smtp_pass'=>config('site.mail_smtp_pass'),
                    'mail_verify_type'=>config('site.mail_verify_type'),
                    'mail_from'=>config('site.mail_from'),
                ];
                $sendcode = generateSalt();
                $html = "<div>您的验证码为：$sendcode</div>";
                // halt($salt);
                \think\Config::set('site', array_merge(\think\Config::get('site'), $row));
                $email = new Email;
                $result = $email
                    ->to($emails)
                    ->subject('fan商城验证')
                    ->message($html)
                    ->send();
                // halt($result);
                if ($result) {
                    // 创建实例
                    $codeManager = new VerifyCodeManager();
                    // 这里是封装了一个方法,存Redis里面
                    $res = $codeManager->setEmailCode((string)$emails, (string)$sendcode);
                    if($res){
                        $this->success('发送验证码成功');
                        exit;
                    }
                } else {
                    $this->error($email->getError());
                    exit;
                }
            }
            
        }
    }

    // 忘记密码-重置密码
    public function resetPwd(){
        if($this->request->isPost()){
            $emails = $this->request->param('email');
            $code = $this->request->param('code');
            $password = $this->request->param('password');
            if($emails==''){
                $this->error('邮箱不能为空');
                exit;
            }
            if($code==''){
                $this->error('验证码不能为空');
                exit;
            }
            if($password==''){
                $this->error('密码不能为空');
                exit;
            }
            // 创建实例
            $codeManager = new VerifyCodeManager();
            // 验证验证码是否正确
            $res = $codeManager->verifyEmailCode((string)$emails, (string)$code);
            // halt($res);
            if($res){
                $saltCode = generateSalt();
                $newPwd = md5($password.$saltCode);
                $updateData = [
                    'salt'=>$saltCode,
                    'password'=>$newPwd,
                ];
                $updateRes = $this->userModel->where('email',$emails)->update($updateData);
                $this->success('重置密码成功，请前往登录页面');
                exit;
                // halt($updateRes,'1111');
            }else{
                $this->error('验证码过期，请重新获取');
                exit;
            }
        }
    }

}

?>

